/**
 * Belongs to WEF
 */

var SelectDialogUtils = {
bindEventFunc:function (config) {
	var parentWindow;
	if(document.all){
		parentWindow = window.dialogArguments;
	} else {
		parentWindow = window.opener;
	}
	config.parentWindow = parentWindow;
	config.currentWindow = window;
	
	var tableId = config.tableId;
	var table = TableUtils._getDomObjById(tableId);
	table.currentRow = null;
	table.currentRowBakConfig = {};	
	var rows = $("#" + tableId + " tr.odd," + "#" + tableId + " tr.even");
	var autoFillConfig = $("#" + config.autoFillFieldId).val();
	// rows of table
	rows.each(function(index, domObj) {
		var clkHandler = $(domObj).attr("onclick");
		// row double click
		$(domObj).bind("dblclick", {
			ownHandler : clkHandler,
			autoFillConfig : autoFillConfig,
			idName : config.idName,
			loadRowService : config.loadRowService,
			loadRowMethod : config.loadRowMethod,
			afterSelectJsFunc : config.afterSelectJsFunc,
			parentWindow : config.parentWindow,
			currentWindow : config.currentWindow
		}, SelectDialogUtils.selectAndAutoFillFunc);
		// row click
		$(domObj).bind("click",{
			table : table,
			row : domObj,
			ownHandler : clkHandler
		},TableUtils._clickRow);		
	});
	
	var btnOk = $("#btnOk");
	if(btnOk){
		btnOk.unbind('click').bind('click',{
			autoFillConfig : autoFillConfig,
			idName : config.idName,
			loadRowService : config.loadRowService,
			loadRowMethod : config.loadRowMethod,
			afterSelectJsFunc : config.afterSelectJsFunc,
			parentWindow : config.parentWindow,
			currentWindow : config.currentWindow
		}, SelectDialogUtils.selectAndAutoFillFunc);
	}
	
	var btnCancel = $("#btnCancel");
	if(btnCancel){
		btnCancel.unbind('click').bind('click',{}, function(){config.currentWindow.close();});
	}
},
// current selected record auto pad to autoFillConfig
selectAndAutoFillFunc:function (event) {
	var context = event.data;
	if (currentRowId == undefined || currentRowId == null
			|| currentRowId == "") {
		//alert(" Please select one record! ");
		alert("请选择一条记录!");
		return false;
	}
	if (context.ownHandler) {
		context.ownHandler();
	}
	var parentWindow = context.parentWindow;
	var arrayAutoFill = context.autoFillConfig.split(',');
	context.loadRowService[context.loadRowMethod](currentRowId, {
		callback : function(port){

			for(var index = 0; index < arrayAutoFill.length; index ++){
				var autoFill = arrayAutoFill[index].split('|');
				var fieldId = autoFill[0];
				var arrayFieldName = autoFill[1].substring(1, autoFill[1].length -1);
				var fieldName = arrayFieldName.split('_'); 
				var fieldValue = '';
				for(var j = 0; j < fieldName.length; j ++){
					fieldValue = fieldValue + port[fieldName[j]] + '/'; 
				}
				fieldValue = fieldValue.substring(0, fieldValue.length-1);
				if(fieldValue == null || fieldValue == undefined
					|| fieldValue == 'UNDEFINED' || fieldValue == 'undefined'
					|| fieldValue == 'NULL' || fieldValue == 'null'){
					fieldValue = ''
				}
				var displayField = $('#'+fieldId+'_code_name_display',parentWindow.document);
				var valueField = $('#'+fieldId,parentWindow.document);
				if(valueField){
					valueField.val(fieldValue);
				}
				if(displayField){
					displayField.val(fieldValue);
					valueField.val(SelectDialogUtils.returnCode(fieldValue));
				}
			}
			if(context.afterSelectJsFunc && context.afterSelectJsFunc != 'null'){
				eval('parentWindow.' + context.afterSelectJsFunc+'()');
			}
			if(IS_FROM_SELECT != null){
				IS_FROM_SELECT[fieldId+"_code_name_display"] = true;
			}
		},
		async : false
	});
	context.currentWindow.close();
},
mergeTwoMap:function (obj1, obj2){
	for(var p in obj1){
		obj2[p] = obj1[p];
	}
	return obj2;
},
clearForm:function (formId){
   var fields=$("#"+formId+" :input");
   fields.each(function(index,domObj){
       var item=$(domObj);
       if(item.attr("name")&&item.attr("type")&&item.attr("type")!='hidden'){
          if(item.attr("type")&&item.attr("type").toLowerCase()=="radio"){	              
                item.removeAttr("checked");	              
          }else if(item.attr("type")&&item.attr("type").toLowerCase()=="checkbox"){	              
                item.removeAttr("checked");
          }else{
             item.val("");
          }
       }
	});
},
getSearchParameterMap:function(searchForm, tableForm){
	var parameterMap1 = SelectDialogUtils.getParameterMap(searchForm);        
	var parameterMap2 = SelectDialogUtils.getParameterMap(tableForm);
	return SelectDialogUtils.mergeTwoMap(parameterMap1, parameterMap2);
},
getTableParameterMap:function(tableForm){
	return SelectDialogUtils.getParameterMap(tableForm);
},
getParameterMap:function (form) {
    var p = document.forms[form].elements;
    var map = new Object();
    for(var x=0; x < p.length; x++) {
        var key = p[x].name;
        var val = p[x].value;
        
        //Check if this field name is unique.
        //If the field name is repeated more than once
        //add it to the current array.
        var curVal = map[key]; 
        if (curVal) { // more than one field so append value to array
        	curVal[curVal.length] = val;
        } else { // add field and value
        	if(val != ''){
        		map[key]= [val];
        	}
        }
    }
    return map;
},
setFormAction:function (form, action, method) {
	if (action) {
		document.forms[form].setAttribute('action', action);
	}
	
	if (method) {
		document.forms[form].setAttribute('method', method);
	}
	
	document.forms[form].ec_eti.value='';
},
returnCode : function(codeName){
	if(codeName && codeName.indexOf('/') != -1){
		return codeName.substring(0,codeName.indexOf('/'));
	}
	return codeName;
}
};